﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HCFramework.API.LINQ;

namespace C
{
    public class TrangRoleController
    {
        public static void Insert(TrangRole a)
        {
            if (Get(a.TrangID, a.RoleID) != null) return;

            HCDataContext db = new HCDataContext();
            db.TrangRoles.InsertOnSubmit(a);
            db.SubmitChanges();
        }

        public static void Update(TrangRole a, HCDataContext db)
        {
            db.SubmitChanges();
        }

        public static void Delete(int id)
        {
            HCDataContext db = new HCDataContext();
            TrangRole a = db.TrangRoles.Where(
                ik => ik.TrangRoleID == id).First();
            if (a.Role.Name == RoleController.GetAdminRoleName()) return;
            db.TrangRoles.DeleteOnSubmit(a);
            db.SubmitChanges();
        }

        public static TrangRole GetByID(int id, HCDataContext db)
        {
            return db.TrangRoles.Where(
                ik => ik.TrangRoleID == id).FirstOrDefault();
        }

        public static TrangRole GetByID(int id)
        {
            return GetByID(id, new HCDataContext());
        }

        public static TrangRole Get(int trangID, int roleID)
        {
            return new HCDataContext().TrangRoles.Where(
                ik => ik.TrangID == trangID && ik.RoleID == roleID).FirstOrDefault();
        }
    }
}